<?php
namespace information\service;

use herosphp\core\Loader;


class BrandService extends CommonService{

    private $brand = null;
    private $adminManagerDao = null;

    public function __construct($brand,$admin) {

        $this->setModelDao(Loader::model($brand));
        $this->adminManagerDao = Loader::model($admin);
    }

    public function getItem($conditions, $fields, $order)
    {
        $item = parent::getItem($conditions, $fields, $order);
        if(!$item) return NULL;
        $admin_manager = $this->adminManagerDao->getItem($item['manager_id']);
        $item['admin_manager_username'] = $admin_manager['username'];
        $item['admin_manager_password'] = $admin_manager['password'];
        $item['admin_manager_mobile'] = $admin_manager['mobile'];
        $item['admin_manager_jobno'] = $admin_manager['jobno'];
        $item['admin_manager_email'] = $admin_manager['email'];
        $item['admin_manager_name'] = $admin_manager['name'];
        $item['admin_manager_role_id'] = $admin_manager['role_id'];
        $item['admin_manager_last_login_time'] = $admin_manager['last_login_time'];
        $item['admin_manager_last_login_ip'] = $admin_manager['last_login_ip'];
        $item['admin_manager_status'] = $admin_manager['status'];
        $item['admin_manager_addtime'] = $admin_manager['addtime'];
        $item['admin_manager_updatetime'] = $admin_manager['updatetime'];
        $item['admin_manager_app_id'] = $admin_manager['app_id'];
        return $item;
    }

    public function getItems($conditions, $fields, $order, $limit, $group, $having)
    {
        if(!$order['addtime'])
        {
            $order['addtime'] = -1;
        }
        $items = parent::getItems($conditions, $fields, $order, $limit, $group, $having);
        if(!$items) return NULL;

        $admin_managercondi = array();
        foreach($items as $k => $v)
        {
            if($v['manager_id'])
            {
                $admin_managercondi[] = $v['manager_id'];
            }
        }

        $admin_managers = $this->adminManagerDao->getItems(array('id' => array( '$in' => $admin_managercondi)));

        /**-- foreignforeach  --**/
        foreach ($items as $k => $v)
        {
            foreach($admin_managers as $kk => $vv)
            {
                if($vv['id'] == $v['manager_id'])
                {
                    $items[$k]['admin_manager_username'] = $vv['username'];
                    $items[$k]['admin_manager_password'] = $vv['password'];
                    $items[$k]['admin_manager_mobile'] = $vv['mobile'];
                    $items[$k]['admin_manager_jobno'] = $vv['jobno'];
                    $items[$k]['admin_manager_email'] = $vv['email'];
                    $items[$k]['admin_manager_name'] = $vv['name'];
                    $items[$k]['admin_manager_role_id'] = $vv['role_id'];
                    $items[$k]['admin_manager_last_login_time'] = $vv['last_login_time'];
                    $items[$k]['admin_manager_last_login_ip'] = $vv['last_login_ip'];
                    $items[$k]['admin_manager_status'] = $vv['status'];
                    $items[$k]['admin_manager_addtime'] = $vv['addtime'];
                    $items[$k]['admin_manager_updatetime'] = $vv['updatetime'];
                    $items[$k]['admin_manager_app_id'] = $vv['app_id'];

                }
            }
        }
        return $items;
    }
}

